package lc;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 三个不同的线程将会共用一个 Foo 实例。
 *
 * 线程 A 将会调用 one() 方法
 * 线程 B 将会调用 two() 方法
 * 线程 C 将会调用 three() 方法
 * 请设计修改程序，以确保 two() 方法在 one() 方法之后被执行，three() 方法在 two() 方法之后被执行。
 *
 */
public class Q1114 {

    private AtomicInteger c = new AtomicInteger(0);

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        while (c.get() != 0) {
            Thread.sleep(1);
        }
        printFirst.run();
        c.getAndIncrement();
    }

    public void second(Runnable printSecond) throws InterruptedException {

        // printSecond.run() outputs "second". Do not change or remove this line.
        while (c.get() != 1) {
            Thread.sleep(1);
        }
        printSecond.run();
        c.getAndIncrement();
    }

    public void third(Runnable printThird) throws InterruptedException {

        // printThird.run() outputs "third". Do not change or remove this line.
        while (c.get() != 2) {
            Thread.sleep(1);
        }
        printThird.run();
        c.getAndIncrement();
    }

}
